package com.njtoyo.taxi.admin.service.business.operation_rule.impl;

import cn.hutool.core.convert.Convert;
import cn.hutool.core.lang.Dict;
import cn.hutool.core.util.ObjectUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.njtoyo.taxi.admin.cache.PrimaryCachePutter;
import com.njtoyo.taxi.admin.mapper.master.entity.DriverCancelRule;
import com.njtoyo.taxi.admin.mapper.master.mapper.DriverCancelRuleMapper;
import com.njtoyo.taxi.admin.rest.wrapper.business.driver.SetDriverCancelRuleFormData;
import com.njtoyo.taxi.admin.service.business.operation_rule.DriverCancelRuleService;
import com.taxi.entity.common.RestResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class DriverCancelRuleServiceImpl extends ServiceImpl<DriverCancelRuleMapper, DriverCancelRule> implements DriverCancelRuleService {
    @Autowired
    private PrimaryCachePutter primaryCachePutter;

    @Override
    public RestResult setDriverCancel(SetDriverCancelRuleFormData wrapper) {
        DriverCancelRule driverCancelRule = new DriverCancelRule();
        driverCancelRule.setDriverRegionId(Convert.toLong(wrapper.getDriverRegionId()));
        driverCancelRule.setFreeCancelTimes(JSON.toJSONString(Dict.create()
                .set(wrapper.getCancelFrequency(), wrapper.getCancelTimes())));
        driverCancelRule.setScoreSpec(JSON.toJSONString(Dict.create()
                .set("driver", wrapper.getDriverCancelScore())
                .set("admin", wrapper.getCsCancelScore())));
        driverCancelRule.setStopAcceptOrderTime(JSON.toJSONString(Dict.create()
                .set("hour", wrapper.getPauseTime())));
        driverCancelRule.setReduceScoreDuration(wrapper.getDeductionDuration());
        driverCancelRule.setIsActive(wrapper.getIsActive());
        if (ObjectUtil.isNotNull(wrapper.getId())) {
            driverCancelRule.setId(wrapper.getId());
            baseMapper.updateById(driverCancelRule);
        } else {
            baseMapper.insert(driverCancelRule);
        }
        //清除缓存
        primaryCachePutter.getRedisTemplate().delete("driver:cancel:rule:" + driverCancelRule.getDriverRegionId());
        return RestResult.success();
    }

    @Override
    public RestResult getDriverCancel(Long driverRegionId) {
        DriverCancelRule driverCancelRule = baseMapper.selectOne(Wrappers.<DriverCancelRule>lambdaQuery()
                .eq(DriverCancelRule::getDriverRegionId, driverRegionId));
        if (ObjectUtil.isNull(driverCancelRule)) {
            return RestResult.success();
        }
        SetDriverCancelRuleFormData res = new SetDriverCancelRuleFormData();
        res.setId(driverCancelRule.getId());
        res.setDriverRegionId(driverCancelRule.getDriverRegionId().toString());
        JSONObject cancelFrequencyJson = JSON.parseObject(driverCancelRule.getFreeCancelTimes());
        res.setCancelFrequency(cancelFrequencyJson.keySet().iterator().next());
        res.setCancelTimes(cancelFrequencyJson.getInteger(res.getCancelFrequency()));
        JSONObject scoreSpecJson = JSON.parseObject(driverCancelRule.getScoreSpec());
        res.setDriverCancelScore(scoreSpecJson.getInteger("driver"));
        res.setCsCancelScore(scoreSpecJson.getInteger("admin"));
        JSONObject stopAcceptOrderTimeJson = JSON.parseObject(driverCancelRule.getStopAcceptOrderTime());
        res.setPauseTime(stopAcceptOrderTimeJson.getInteger("hour"));
        res.setDeductionDuration(driverCancelRule.getReduceScoreDuration());
        res.setIsActive(driverCancelRule.getIsActive());
        return RestResult.success(res);
    }
}
